<?php

namespace app\shop\service\order;

use app\shop\model\store\Order;
use app\shop\model\order\Order as OrderModel;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

/**
 * 订单导出服务类
 */
class ExportService
{
    /**
     * 订单导出
     */
    public function orderListBlack($list)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //列宽
        $sheet->getColumnDimension('B')->setWidth(30);
        $sheet->getColumnDimension('P')->setWidth(30);

        //设置工作表标题名称
        $sheet->setTitle('订单明细');

        $sheet->setCellValue('A1', '订单号');
        $sheet->setCellValue('B1', '商品信息');
        $sheet->setCellValue('C1', '订单总额');
        $sheet->setCellValue('D1', '优惠券抵扣');
        $sheet->setCellValue('E1', '积分抵扣');
        $sheet->setCellValue('F1', '运费金额');
        $sheet->setCellValue('G1', '后台改价');
        $sheet->setCellValue('H1', '实付款金额');
        $sheet->setCellValue('I1', '支付方式');
        $sheet->setCellValue('J1', '下单时间');
        $sheet->setCellValue('K1', '买家');
        $sheet->setCellValue('L1', '买家留言');
        $sheet->setCellValue('M1', '配送方式');
        $sheet->setCellValue('N1', '自提门店名称');
        $sheet->setCellValue('O1', '自提联系人');
        $sheet->setCellValue('P1', '自提联系电话');
        $sheet->setCellValue('Q1', '收货人姓名');
        $sheet->setCellValue('R1', '联系电话');
        $sheet->setCellValue('S1', '收货人地址');
        $sheet->setCellValue('T1', '物流公司');
        $sheet->setCellValue('U1', '物流单号');
        $sheet->setCellValue('V1', '付款状态');
        $sheet->setCellValue('W1', '付款时间');
        $sheet->setCellValue('X1', '发货状态');
        $sheet->setCellValue('Y1', '发货时间');
        $sheet->setCellValue('Z1', '收货状态');
        $sheet->setCellValue('AA1', '收货时间');
        $sheet->setCellValue('AB1', '订单状态');
        $sheet->setCellValue('AC1', '微信支付交易号');
        $sheet->setCellValue('AD1', '是否已评价');

        //填充数据
        $index = 0;
        $startline = 2;
        foreach ($list as $order) {
            $address = $order['address'];
            $sheet->setCellValue('A' . ($index + $startline), "\t" . $order['order_no'] . "\t");
            $sheet->setCellValue('B' . ($index + $startline), $this->filterProductInfo($order));
            $sheet->setCellValue('C' . ($index + $startline), $order['total_price']);
            $sheet->setCellValue('D' . ($index + $startline), $order['coupon_money']);
            $sheet->setCellValue('E' . ($index + $startline), $order['points_money']);
            $sheet->setCellValue('F' . ($index + $startline), $order['express_price']);
            $sheet->setCellValue('G' . ($index + $startline), "{$order['update_price']['symbol']}{$order['update_price']['value']}");
            $sheet->setCellValue('H' . ($index + $startline), $order['order_source'] == 70 ? round($order['pay_price'] + $order['advance']['pay_price'], 2) : $order['pay_price']);
            $sheet->setCellValue('I' . ($index + $startline), $order['pay_type']['text']);
            $sheet->setCellValue('J' . ($index + $startline), $order['create_time']);
            $sheet->setCellValue('K' . ($index + $startline), $order['user']['nickName']);
            $sheet->setCellValue('L' . ($index + $startline), $order['buyer_remark']);
            $sheet->setCellValue('M' . ($index + $startline), $order['delivery_type']['text']);
            $sheet->setCellValue('N' . ($index + $startline), !empty($order['extract_store']) ? $order['extract_store']['shop_name'] : '');
            $sheet->setCellValue('O' . ($index + $startline), !empty($order['extract']) ? $order['extract']['linkman'] : '');
            $sheet->setCellValue('P' . ($index + $startline), !empty($order['extract']) ? $order['extract']['phone'] : '');
            $sheet->setCellValue('Q' . ($index + $startline), !empty($order['address']) ? $order['address']['name'] : '');
            $sheet->setCellValue('R' . ($index + $startline), !empty($order['address']) ? $order['address']['phone'] : '');
            $sheet->setCellValue('S' . ($index + $startline), $address ? $address->getFullAddress() : '');
            $sheet->setCellValue('T' . ($index + $startline), !empty($order['express']) ? $order['express']['express_name'] : '');
            $sheet->setCellValue('U' . ($index + $startline), $order['express_no']);
            $sheet->setCellValue('V' . ($index + $startline), $order['pay_status']['text']);
            $sheet->setCellValue('W' . ($index + $startline), $this->filterTime($order['pay_time']));
            $sheet->setCellValue('X' . ($index + $startline), $order['delivery_status']['text']);
            $sheet->setCellValue('Y' . ($index + $startline), $this->filterTime($order['delivery_time']));
            $sheet->setCellValue('Z' . ($index + $startline), $order['receipt_status']['text']);
            $sheet->setCellValue('AA' . ($index + $startline), $this->filterTime($order['receipt_time']));
            $sheet->setCellValue('AB' . ($index + $startline), $order['order_status']['text']);
            $sheet->setCellValue('AC' . ($index + $startline), $order['transaction_id']);
            $sheet->setCellValue('AD' . ($index + $startline), $order['is_comment'] ? '是' : '否');
            $index++;
        }

        //保存文件
        $writer = new Xlsx($spreadsheet);
        $filename = iconv("UTF-8", "GB2312//IGNORE", '订单') . '-' . date('YmdHis') . '.xlsx';


        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit();
    }

    public function orderList($list, $mergeC = true, OrderModel $orderModel = null)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //列宽
        $sheet->getColumnDimension('B')->setWidth(30);
        $sheet->getColumnDimension('P')->setWidth(30);

        //设置工作表标题名称
        $sheet->setTitle('订单明细');

        $sheet->setCellValue('A1', '订单号');
        $sheet->setCellValue('B1', '商品信息');
        $sheet->setCellValue('C1', '商品规格');
        $sheet->setCellValue('D1', '商品单价');
        $sheet->setCellValue('E1', '购买数量');
        $sheet->setCellValue('F1', '商品总价');
        $sheet->setCellValue('G1', '订单总额');
        $sheet->setCellValue('H1', '优惠券抵扣');
        $sheet->setCellValue('I1', '实付款金额');
        $sheet->setCellValue('J1', '支付方式');
        $sheet->setCellValue('K1', '买家');
        $sheet->setCellValue('L1', '买家ID');
        $sheet->setCellValue('M1', '买家留言');
        $sheet->setCellValue('N1', '收货人姓名');
        $sheet->setCellValue('O1', '联系电话');
        $sheet->setCellValue('P1', '收货人地址');
        $sheet->setCellValue('Q1', '下单时间');
        $sheet->setCellValue('R1', '付款时间');
        $sheet->setCellValue('S1', '付款状态');
        $sheet->setCellValue('T1', '发货时间');
        $sheet->setCellValue('U1', '收货状态');
        $sheet->setCellValue('V1', '收货时间');
        $sheet->setCellValue('W1', '订单状态');
        $sheet->setCellValue('X1', '是否已评价');
        $sheet->setCellValue('Y1', '商户');
        $sheet->setCellValue('Z1', '门店');
        $sheet->setCellValue('AA1', '骑手姓名');
        $sheet->setCellValue('AB1', '骑手电话');
        $sheet->setCellValue('AC1', '是否首单');


        //填充数据
        $index = 0;
        $startline = 2;

        $orderModel = $orderModel ?: new OrderModel();
        foreach ($list as $order) {
            $address = $order['address'];
            $inside_line = 0;

            $order['store_clerk']= $orderModel->getStoreClerk($order);
            $content = '';
            foreach ($order['product'] as $key => $product) {
                $sheet->setCellValue('B' . ($index + $startline + $inside_line), $product['product_name']);
                $sheet->setCellValue('C' . ($index + $startline + $inside_line), $product['product_attr']);
                $sheet->setCellValue('D' . ($index + $startline + $inside_line), $product['product_price']);
                $sheet->setCellValue('E' . ($index + $startline + $inside_line), $product['total_num']);
                $sheet->setCellValue('F' . ($index + $startline + $inside_line), $product['total_price']);
                $inside_line++;
            }
            $sheet->setCellValue('A' . ($index + $startline), "\t" . $order['order_no'] . "\t");
            // $sheet->setCellValue('B' . ($index + $startline), $this->filterProductInfo($order));
            $sheet->setCellValue('G' . ($index + $startline), $order['total_price']);
            $sheet->setCellValue('H' . ($index + $startline), $order['reduce_price']);
            $sheet->setCellValue('I' . ($index + $startline), $order['order_source'] == 70 ? round($order['pay_price'] + $order['advance']['pay_price'], 2) : $order['pay_price']);
            $sheet->setCellValue('J' . ($index + $startline), '微信');

            $sheet->setCellValue('K' . ($index + $startline), $order['user']['nickName']);
            $sheet->setCellValue('L' . ($index + $startline), $order['user']['user_id']);
            $sheet->setCellValue('M' . ($index + $startline), $order['buyer_remark']);
            $sheet->setCellValue('N' . ($index + $startline), !empty($order['address']) ? $order['address']['name'] : '');
            $sheet->setCellValue('O' . ($index + $startline), !empty($order['address']) ? $order['address']['phone'] : '');

            //kevin 2024/5/16
            $sheet->setCellValue('P' . ($index + $startline), $address ? $address->getFullAddress() : '');
            $sheet->setCellValue('Q' . ($index + $startline), $order['create_time']);
            $sheet->setCellValue('R' . ($index + $startline), !empty($order['pay_time']) ? $this->filterTime($order['pay_time']) : '');
            $sheet->setCellValue('S' . ($index + $startline), $order['pay_status']['text']);

            $sheet->setCellValue('T' . ($index + $startline), !empty($order['delivery_time']) ? $this->filterTime($order['delivery_time']) : '');
            $sheet->setCellValue('U' . ($index + $startline), $order['receipt_status']['text']);
            $sheet->setCellValue('V' . ($index + $startline), !empty($order['receipt_time']) ? $order['receipt_time'] : '');
            $sheet->setCellValue('W' . ($index + $startline), $order['state_text']);
            $sheet->setCellValue('X' . ($index + $startline), $order['is_comment'] ? '是' : '否');

            $sheet->setCellValue('Y' . ($index + $startline), $order['supplier']['name']?? '');//商户
            $sheet->setCellValue('Z' . ($index + $startline), isset($order['storeInfo']['store_name']) ? $order['storeInfo']['store_name'] : '');
            $sheet->setCellValue('AA' . ($index + $startline), $order['store_clerk']['name']?? '');
            $sheet->setCellValue('AB' . ($index + $startline), $order['store_clerk']['phone']?? '');
            $sheet->setCellValue('AC' . ($index + $startline), $order['is_first_order'] ? '是' : '否');

            if ($inside_line > 1) {
                if ($mergeC == false) {
                    $styleArray = [
                        'alignment' => [
                            // 垂直居中
                            'vertical' => Alignment::VERTICAL_CENTER
                        ],
                    ];
                    for ($ii = 1; $ii <= $inside_line - 1; $ii++) {
                        $sheet->setCellValue('A' . ($index + $startline + $ii), "");

                        $sheet->setCellValue('G' . ($index + $startline + $ii), $order['total_price']);
                        $sheet->setCellValue('H' . ($index + $startline + $ii), $order['coupon_money']);
                        $sheet->setCellValue('I' . ($index + $startline + $ii), $order['order_source'] == 70 ? round($order['pay_price'] + $order['advance']['pay_price'], 2) : $order['pay_price']);
                        $sheet->setCellValue('J' . ($index + $startline + $ii), '微信');

                        $sheet->setCellValue('L' . ($index + $startline + $ii), $order['user']['nickName']);
                        $sheet->setCellValue('M' . ($index + $startline + $ii), $order['user']['user_id']);
                        $sheet->setCellValue('N' . ($index + $startline + $ii), $order['buyer_remark']);
                        $sheet->setCellValue('O' . ($index + $startline + $ii), !empty($order['address']) ? $order['address']['name'] : '');
                        $sheet->setCellValue('P' . ($index + $startline + $ii), !empty($order['address']) ? $order['address']['phone'] : '');

                        //kevin 2024/5/16
                        $sheet->setCellValue('S' . ($index + $startline + $ii), $address ? $address->getFullAddress() : '');
                        $sheet->setCellValue('K' . ($index + $startline + $ii), $order['create_time']);
                        $sheet->setCellValue('U' . ($index + $startline + $ii), !empty($order['pay_time']) ? $this->filterTime($order['pay_time']) : '');
                        $sheet->setCellValue('T' . ($index + $startline + $ii), $order['pay_status']['text']);

                        $sheet->setCellValue('V' . ($index + $startline + $ii), !empty($order['delivery_time']) ? $this->filterTime($order['delivery_time']) : '');
                        $sheet->setCellValue('W' . ($index + $startline + $ii), $order['receipt_status']['text']);
                        $sheet->setCellValue('X' . ($index + $startline + $ii), !empty($order['receipt_time']) ? $order['receipt_time'] : '');
                        $sheet->setCellValue('Y' . ($index + $startline + $ii), $order['order_status']['text']);
                        $sheet->setCellValue('Z' . ($index + $startline + $ii), $order['is_comment'] ? '是' : '否');

                        $sheet->setCellValue('Y' . ($index + $startline), $order['supplier']['name']?? '');//商户
                        $sheet->setCellValue('Z' . ($index + $startline + $ii), isset($order['storeInfo']['store_name']) ? $order['storeInfo']['store_name'] : '');
                        $sheet->setCellValue('AA' . ($index + $startline), $order['store_clerk']['name']?? '');
                        $sheet->setCellValue('AB' . ($index + $startline), $order['store_clerk']['phone']?? '');
                        $sheet->setCellValue('AC' . ($index + $startline + $ii), $order['is_first_order'] ? '是' : '否');
                        $end_ii = $ii;
                    }
                    $sheet->mergeCells('A' . ($index + $startline) . ':' . 'A' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);

                    $sheet->getStyle('A' . ($index + $startline) . ':' . 'A' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $startline += $end_ii;
                } else {
                    $end_ii = 0;
                    $styleArray = [
                        'alignment' => [
                            // 垂直居中
                            'vertical' => Alignment::VERTICAL_CENTER
                        ],
                    ];
                    for ($ii = 1; $ii <= $inside_line - 1; $ii++) {
                        $sheet->setCellValue('A' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('G' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('H' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('I' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('J' . ($index + $startline + $ii), "");

                        $sheet->setCellValue('L' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('M' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('N' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('O' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('P' . ($index + $startline + $ii), "");

                        //kevin 2024/5/16
                        $sheet->setCellValue('S' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('K' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('U' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('T' . ($index + $startline + $ii), "");

                        $sheet->setCellValue('V' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('W' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('X' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('Y' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('Z' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('AA' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('Q' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('R' . ($index + $startline + $ii), "");
                        $sheet->setCellValue('AB' . ($index + $startline + $ii), "");
                        $end_ii = $ii;
                    }
                    $sheet->mergeCells('A' . ($index + $startline) . ':' . 'A' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('G' . ($index + $startline) . ':' . 'G' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('H' . ($index + $startline) . ':' . 'H' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('I' . ($index + $startline) . ':' . 'I' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('J' . ($index + $startline) . ':' . 'J' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);

                    $sheet->mergeCells('L' . ($index + $startline) . ':' . 'L' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('M' . ($index + $startline) . ':' . 'M' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('N' . ($index + $startline) . ':' . 'N' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('O' . ($index + $startline) . ':' . 'O' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('P' . ($index + $startline) . ':' . 'P' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);

                    //kevin 2024/5/16
                    $sheet->mergeCells('S' . ($index + $startline) . ':' . 'S' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('K' . ($index + $startline) . ':' . 'K' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('U' . ($index + $startline) . ':' . 'U' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('T' . ($index + $startline) . ':' . 'T' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);

                    $sheet->mergeCells('V' . ($index + $startline) . ':' . 'V' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('W' . ($index + $startline) . ':' . 'W' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('X' . ($index + $startline) . ':' . 'X' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('Y' . ($index + $startline) . ':' . 'Y' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('Z' . ($index + $startline) . ':' . 'Z' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('AA' . ($index + $startline) . ':' . 'AA' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('Q' . ($index + $startline) . ':' . 'Q' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('R' . ($index + $startline) . ':' . 'R' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);
                    $sheet->mergeCells('AB' . ($index + $startline) . ':' . 'AB' . ($index + $startline + $end_ii))->getCellByColumnAndRow(1, 1);

                    $sheet->getStyle('A' . ($index + $startline) . ':' . 'A' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('G' . ($index + $startline) . ':' . 'G' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('H' . ($index + $startline) . ':' . 'H' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('I' . ($index + $startline) . ':' . 'I' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('J' . ($index + $startline) . ':' . 'J' . ($index + $startline + $end_ii))->applyFromArray($styleArray);

                    $sheet->getStyle('L' . ($index + $startline) . ':' . 'L' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('M' . ($index + $startline) . ':' . 'M' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('N' . ($index + $startline) . ':' . 'N' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('O' . ($index + $startline) . ':' . 'O' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('P' . ($index + $startline) . ':' . 'P' . ($index + $startline + $end_ii))->applyFromArray($styleArray);

                    //kevin 2024/5/16
                    $sheet->getStyle('S' . ($index + $startline) . ':' . 'S' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('K' . ($index + $startline) . ':' . 'K' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('U' . ($index + $startline) . ':' . 'U' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('T' . ($index + $startline) . ':' . 'T' . ($index + $startline + $end_ii))->applyFromArray($styleArray);

                    $sheet->getStyle('V' . ($index + $startline) . ':' . 'V' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('W' . ($index + $startline) . ':' . 'W' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('X' . ($index + $startline) . ':' . 'X' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('Y' . ($index + $startline) . ':' . 'Y' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('Z' . ($index + $startline) . ':' . 'Z' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('AA' . ($index + $startline) . ':' . 'AA' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('Q' . ($index + $startline) . ':' . 'Q' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('R' . ($index + $startline) . ':' . 'R' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('AB' . ($index + $startline) . ':' . 'AB' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $sheet->getStyle('AC' . ($index + $startline) . ':' . 'AC' . ($index + $startline + $end_ii))->applyFromArray($styleArray);
                    $startline += $end_ii;
                }
            }
            $index++;
        }

        //保存文件
        $writer = new Xlsx($spreadsheet);
        $filename = iconv("UTF-8", "GB2312//IGNORE", '订单') . '-' . date('YmdHis') . '.xlsx';


        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit();
    }

    /**
     * 分销订单导出
     */
    public function agentOrderList($list)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //列宽
        $sheet->getColumnDimension('B')->setWidth(30);

        //设置工作表标题名称
        $sheet->setTitle('分销订单明细');

        $sheet->setCellValue('A1', '订单号');
        $sheet->setCellValue('B1', '商品信息');
        $sheet->setCellValue('C1', '订单总额');
        $sheet->setCellValue('D1', '实付款金额');
        $sheet->setCellValue('E1', '支付方式');
        $sheet->setCellValue('F1', '下单时间');
        $sheet->setCellValue('G1', '一级分销商');
        $sheet->setCellValue('H1', '一级分销佣金');
        $sheet->setCellValue('I1', '二级分销商');
        $sheet->setCellValue('J1', '二级分销佣金');
        $sheet->setCellValue('K1', '三级分销商');
        $sheet->setCellValue('L1', '三级分销佣金');
        $sheet->setCellValue('M1', '买家');
        $sheet->setCellValue('N1', '付款状态');
        $sheet->setCellValue('O1', '付款时间');
        $sheet->setCellValue('P1', '发货状态');
        $sheet->setCellValue('Q1', '发货时间');
        $sheet->setCellValue('R1', '收货状态');
        $sheet->setCellValue('S1', '收货时间');
        $sheet->setCellValue('T1', '订单状态');
        $sheet->setCellValue('U1', '佣金结算');
        $sheet->setCellValue('V1', '结算时间');
        //填充数据
        $index = 0;
        foreach ($list as $agent) {
            $order = $agent['order_master'];
            $sheet->setCellValue('A' . ($index + 2), "\t" . $order['order_no'] . "\t");
            $sheet->setCellValue('B' . ($index + 2), $this->filterProductInfo($order));
            $sheet->setCellValue('C' . ($index + 2), $order['total_price']);
            $sheet->setCellValue('D' . ($index + 2), $order['pay_price']);
            $sheet->setCellValue('E' . ($index + 2), $order['pay_type']['text']);
            $sheet->setCellValue('F' . ($index + 2), $order['create_time']);
            $sheet->setCellValue('G' . ($index + 2), isset($agent['agent_first']) ? $agent['agent_first']['nickName'] : '');
            $sheet->setCellValue('H' . ($index + 2), $agent['first_money']);
            $sheet->setCellValue('I' . ($index + 2), isset($agent['agent_second']) ? $agent['agent_second']['nickName'] : '');
            $sheet->setCellValue('J' . ($index + 2), $agent['second_money']);
            $sheet->setCellValue('K' . ($index + 2), isset($agent['agent_third']) ? $agent['agent_third']['nickName'] : '');
            $sheet->setCellValue('L' . ($index + 2), $agent['third_money']);
            $sheet->setCellValue('M' . ($index + 2), $order['user']['nickName']);
            $sheet->setCellValue('N' . ($index + 2), $order['pay_status']['text']);
            $sheet->setCellValue('O' . ($index + 2), $this->filterTime($order['pay_time']));
            $sheet->setCellValue('P' . ($index + 2), $order['delivery_status']['text']);
            $sheet->setCellValue('Q' . ($index + 2), $this->filterTime($order['delivery_time']));
            $sheet->setCellValue('R' . ($index + 2), $order['receipt_status']['text']);
            $sheet->setCellValue('S' . ($index + 2), $this->filterTime($order['receipt_time']));
            $sheet->setCellValue('T' . ($index + 2), $order['order_status']['text']);
            $sheet->setCellValue('U' . ($index + 2), $agent['is_settled'] == 1 ? '已结算' : '未结算');
            $sheet->setCellValue('V' . ($index + 2), $this->filterTime($agent['settle_time']));
            $index++;
        }

        //保存文件
        $filename = iconv("UTF-8", "GB2312//IGNORE", '分销订单') . '-' . date('YmdHis') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit();
    }

    /**
     * 提现订单导出
     */
    public function cashList($list)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //列宽
        $sheet->getColumnDimension('H')->setWidth(50);

        //设置工作表标题名称
        $sheet->setTitle('提现明细');

        $sheet->setCellValue('A1', 'ID');
        $sheet->setCellValue('B1', '分销商id');
        $sheet->setCellValue('C1', '分销商姓名');
        $sheet->setCellValue('D1', '微信昵称');
        $sheet->setCellValue('E1', '手机号');
        $sheet->setCellValue('F1', '提现金额');
        $sheet->setCellValue('G1', '提现方式');
        $sheet->setCellValue('H1', '提现信息');
        $sheet->setCellValue('I1', '审核状态');
        $sheet->setCellValue('J1', '申请时间');
        $sheet->setCellValue('K1', '审核时间');
        //填充数据
        $index = 0;
        foreach ($list as $cash) {
            $sheet->setCellValue('A' . ($index + 2), $cash['id']);
            $sheet->setCellValue('B' . ($index + 2), $cash['user_id']);
            $sheet->setCellValue('C' . ($index + 2), $cash['real_name']);
            $sheet->setCellValue('D' . ($index + 2), $cash['nickName']);
            $sheet->setCellValue('E' . ($index + 2), "\t" . $cash['mobile'] . "\t");
            $sheet->setCellValue('F' . ($index + 2), $cash['money']);
            $sheet->setCellValue('G' . ($index + 2), $cash['pay_type']['text']);
            $sheet->setCellValue('H' . ($index + 2), $this->cashInfo($cash));
            $sheet->setCellValue('I' . ($index + 2), $cash['apply_status']['text']);
            $sheet->setCellValue('J' . ($index + 2), $cash['create_time']);
            $sheet->setCellValue('K' . ($index + 2), $cash['audit_time']);
            $index++;
        }
        //保存文件
        $filename = iconv("UTF-8", "GB2312//IGNORE", '提现明细') . '-' . date('YmdHis') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit();
    }

    /**
     * 余额提现订单导出
     */
    public function userCashList($list)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //列宽
        $sheet->getColumnDimension('I')->setWidth(50);

        //设置工作表标题名称
        $sheet->setTitle('余额提现明细');

        $sheet->setCellValue('A1', 'ID');
        $sheet->setCellValue('B1', '用户ID');
        $sheet->setCellValue('C1', '微信昵称');
        $sheet->setCellValue('D1', '手机号');
        $sheet->setCellValue('E1', '提现金额');
        $sheet->setCellValue('F1', '实际到账');
        $sheet->setCellValue('G1', '提现比例');
        $sheet->setCellValue('H1', '提现方式');
        $sheet->setCellValue('I1', '提现信息');
        $sheet->setCellValue('J1', '审核状态');
        $sheet->setCellValue('K1', '申请时间');
        $sheet->setCellValue('L1', '审核时间');
        //填充数据
        $index = 0;
        foreach ($list as $cash) {
            $sheet->setCellValue('A' . ($index + 2), $cash['id']);
            $sheet->setCellValue('B' . ($index + 2), $cash['user_id']);
            $sheet->setCellValue('C' . ($index + 2), $cash['nickName']);
            $sheet->setCellValue('D' . ($index + 2), "\t" . $cash['mobile'] . "\t");
            $sheet->setCellValue('E' . ($index + 2), $cash['money']);
            $sheet->setCellValue('F' . ($index + 2), $cash['real_money']);
            $sheet->setCellValue('G' . ($index + 2), $cash['cash_ratio'] . '%');
            $sheet->setCellValue('H' . ($index + 2), $cash['pay_type']['text']);
            $sheet->setCellValue('I' . ($index + 2), $this->cashInfo($cash));
            $sheet->setCellValue('J' . ($index + 2), $cash['apply_status']['text']);
            $sheet->setCellValue('K' . ($index + 2), $cash['create_time']);
            $sheet->setCellValue('L' . ($index + 2), $cash['audit_time']);
            $index++;
        }
        //保存文件
        $filename = iconv("UTF-8", "GB2312//IGNORE", '余额提现明细') . '-' . date('YmdHis') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit();
    }

    /**
     * 抽奖记录导出
     */
    public function lotteryList($list)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //列宽
        $sheet->getColumnDimension('E')->setWidth(20);
        $sheet->getColumnDimension('I')->setWidth(20);

        //设置工作表标题名称
        $sheet->setTitle('抽奖记录明细');

        $sheet->setCellValue('A1', 'ID');
        $sheet->setCellValue('B1', '用户ID');
        $sheet->setCellValue('C1', '用户昵称');
        $sheet->setCellValue('D1', '手机号');
        $sheet->setCellValue('E1', '中奖内容');
        $sheet->setCellValue('F1', '中奖类型');
        $sheet->setCellValue('G1', '状态');
        $sheet->setCellValue('H1', '收货人姓名');
        $sheet->setCellValue('I1', '收件人电话');
        $sheet->setCellValue('J1', '收货地址');
        $sheet->setCellValue('K1', '物流公司');
        $sheet->setCellValue('L1', '物流单号');
        $sheet->setCellValue('M1', '发货状态');
        $sheet->setCellValue('N1', '发货时间');
        $sheet->setCellValue('O1', '抽奖时间');
        //填充数据
        $index = 0;
        foreach ($list as $item) {
            $sheet->setCellValue('A' . ($index + 2), $item['record_id']);
            $sheet->setCellValue('B' . ($index + 2), $item['user_id']);
            $sheet->setCellValue('C' . ($index + 2), $item['user'] ? $item['user']['nickName'] : '');
            $sheet->setCellValue('D' . ($index + 2), $item['user'] ? "\t" . $item['user']['mobile'] . "\t" : '');
            $sheet->setCellValue('E' . ($index + 2), $item['record_name']);
            $sheet->setCellValue('F' . ($index + 2), $item['lottery_type_text']);
            $sheet->setCellValue('G' . ($index + 2), $item['status'] == 1 ? '已兑换' : '未兑换');
            $sheet->setCellValue('H' . ($index + 2), $item['name']);
            $sheet->setCellValue('I' . ($index + 2), $item['phone']);
            $sheet->setCellValue('J' . ($index + 2), $item['province_id'] && $item['region'] ? $item['region']['province'] . $item['region']['city'] . $item['region']['region'] . $item['detail'] : '');
            $sheet->setCellValue('K' . ($index + 2), $item['express'] ? $item['express']['express_name'] : '');
            $sheet->setCellValue('L' . ($index + 2), $item['express_no']);
            $sheet->setCellValue('M' . ($index + 2), $item['delivery_status'] == 10 ? '未发货' : '已发货');
            $sheet->setCellValue('N' . ($index + 2), $item['delivery_time']);
            $sheet->setCellValue('O' . ($index + 2), $item['create_time']);
            $index++;
        }
        //保存文件
        $filename = iconv("UTF-8", "GB2312//IGNORE", '抽奖记录明细') . '-' . date('YmdHis') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit();
    }

    /**
     * 格式化提现信息
     */
    private function cashInfo($cash)
    {
        $content = '';
        if ($cash['pay_type']['value'] == 20) {
            $content .= "支付宝姓名：{$cash['alipay_name']}\n";
            $content .= "  支付宝账号：{$cash['alipay_account']}\n";
        } elseif ($cash['pay_type']['value'] == 30) {
            $content .= "银行名称：{$cash['bank_name']}\n";
            $content .= "  开户名：{$cash['bank_account']}\n";
            $content .= "  银行卡号：{$cash['bank_card']}\n";
        }
        return $content;
    }

    /**
     * 格式化商品信息
     */
    private function filterProductInfo($order)
    {
        $content = '';
        foreach ($order['product'] as $key => $product) {
            $content .= ($key + 1) . ".商品名称：{$product['product_name']}\n";
            !empty($product['product_attr']) && $content .= "　商品规格：{$product['product_attr']}\n";
            $content .= "　购买数量：{$product['total_num']}\n";
            $content .= "　商品总价：{$product['total_price']}元\n\n";
        }
        return $content;
    }


    /**
     * 日期值过滤
     */
    private function filterTime($value)
    {
        if (!$value) return '';
        return date('Y-m-d H:i:s', $value);
    }

}
